package com.nexen.merchant.serviceRequest.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.core.domain.model.LoginUser;
import com.nexen.common.utils.SecurityUtils;
import com.nexen.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nexen.common.annotation.Log;
import com.nexen.common.core.controller.BaseController;
import com.nexen.common.core.domain.AjaxResult;
import com.nexen.common.enums.BusinessType;
import com.nexen.merchant.serviceRequest.domain.NexenServicerequest;
import com.nexen.merchant.serviceRequest.service.INexenServicerequestService;
import com.nexen.common.utils.poi.ExcelUtil;
import com.nexen.common.core.page.TableDataInfo;

/**
 * 换胎申请Controller
 * 
 * @author xiao
 * @date 2024-08-03
 */
@RestController
@RequestMapping("/merchant/servicerRquest")
public class NexenServicerequestController extends BaseController
{
    @Autowired
    private INexenServicerequestService nexenServicerequestService;

    /**
     * 查询换胎申请列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexenServicerequest nexenServicerequest)
    {
        startPage();
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                String cityIds = currentUser.getCityIds();
                if (StringUtils.isNotBlank(cityIds)){
                    List<String> cityList = Arrays.asList(cityIds.split(";"));
                    if (cityList != null && cityList.size() > 0) {
                        nexenServicerequest.setCityList(cityList);
                    }
                }
            }
        }

        List<NexenServicerequest> list = nexenServicerequestService.selectNexenServicerequestList(nexenServicerequest);
        return getDataTable(list);
    }

    /**
     * 导出换胎申请列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:export')")
    @Log(title = "换胎申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexenServicerequest nexenServicerequest)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                String cityIds = currentUser.getCityIds();
                if (StringUtils.isNotBlank(cityIds)){
                    List<String> cityList = Arrays.asList(cityIds.split(";"));
                    if (cityList != null && cityList.size() > 0) {
                        nexenServicerequest.setCityList(cityList);
                    }
                }
            }
        }

        List<NexenServicerequest> list = nexenServicerequestService.selectNexenServicerequestList(nexenServicerequest);
        ExcelUtil<NexenServicerequest> util = new ExcelUtil<NexenServicerequest>(NexenServicerequest.class);
        util.exportExcel(response, list, "换胎申请数据");
    }

    /**
     * 获取换胎申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(nexenServicerequestService.selectNexenServicerequestById(Id));
    }

    /**
     * 新增换胎申请
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:add')")
    @Log(title = "换胎申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexenServicerequest nexenServicerequest)
    {
        return toAjax(nexenServicerequestService.insertNexenServicerequest(nexenServicerequest));
    }

    /**
     * 修改换胎申请
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:edit')")
    @Log(title = "换胎申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexenServicerequest nexenServicerequest)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenServicerequest.setUpdateBy(sysUser.getNickName());
        }
        return toAjax(nexenServicerequestService.updateNexenServicerequest(nexenServicerequest));
    }

    /**
     * 审核换胎申请
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:edit')")
    @Log(title = "换胎申请审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult editAudit(@RequestBody NexenServicerequest nexenServicerequest)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenServicerequest.setAuditBy(sysUser.getNickName());
            nexenServicerequest.setUpdateBy(sysUser.getNickName());
        }
        return toAjax(nexenServicerequestService.updateAuditNexenServicerequest(nexenServicerequest));
    }

    /**
     * 删除换胎申请
     */
    @PreAuthorize("@ss.hasPermi('merchant:servicerRquest:remove')")
    @Log(title = "换胎申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(nexenServicerequestService.deleteNexenServicerequestByIds(Ids));
    }
}
